#!/bin/bash
# init scripts special for vmware under Arch GNU/Linux
# Archlive <http://archlive-pkg.googlecode.com>

. /etc/rc.conf
. /etc/rc.d/functions
. /etc/profile.d/vmware.sh

ETCDIR="/etc/vmware"
MODULES="vmblock vmci vmmon vmnet vsock"
. $ETCDIR/bootstrap
sourcedir="$LIBDIR"/vmware/modules/source
kernver=$(uname -r)

DIR=$(pwd)
build_in_tmp() {
	workdir=$(mktemp -du /tmp/vmware-modules.XXXXX)
	cp -a "$sourcedir"  "${workdir}/"
	cd "${workdir}"
	for mod in $MODULES; do
		tar -xpf $mod.tar -C ./
	done
	case $kernver in 2.6.39*)
		patch -Np1 -i vmware2.6.39.patch;;
	esac
	for mod in $MODULES; do
		pushd ${mod}-only && make 2>&1 >>/var/log/vmware.log && install -Dm755 $mod.ko /lib/modules/$kernver/misc/$mod.ko && popd
	done
	cd $DIR
	rm -rf "${workdir}"
	depmod -a
}

setup() {
	stat_busy "Starting Build VMware Kernel Modules" | tee -a /var/log/vmware.log
	build_in_tmp 2>&1 >/dev/null
	for mod in $MODULES; do
		if [ ! -e /lib/modules/$kernver/misc/$mod.ko ]; then
			stat_die 
		fi
	done
	cat >/etc/udev/rules.d/60-vmware.rules <<-EOF
		KERNEL=="vmci",  GROUP="vmware", MODE=660
		KERNEL=="vmmon", GROUP="vmware", MODE=660
		KERNEL=="vsock", GROUP="vmware", MODE=660
	EOF
	stat_done
}
init_net() {
	seconds=5
	if [ ! -e /etc/vmware/networking ]; then
		stat_busy "Initlizing VMware Networks" | tee -a /var/log/vmware.log
		while [ ! -e /etc/vmware/networking -a ${seconds} -gt 0 ]; do
			vmware-networks --postinstall vmware-workstation,old,new 2>&1 >>/var/log/vmware.log
			sleep 1
			seconds=$((${seconds}-1))
		done
		if [ -e /etc/vmware/networking ]; then
			stat_done
		else
			stat_fail
		fi
	fi
}
start() {
	local have_module
	have_module=y
	for mod in $MODULES; do
		if [ ! -e /lib/modules/$kernver/misc/$mod.ko ]; then
			have_module=n
		fi
	done
	if [ "${have_module}" = "n" ]; then
		setup
	fi

	stat_busy "Starting VMware USB Arbitrator" | tee -a /var/log/vmware.log
	vmware-usbarbitrator  2>&1 >>/var/log/vmware.log
	if [ $? -gt 0 ]; then
		stat_fail
		exit 1
	else
		stat_done
	fi

	modprobe -a $MODULES
	init_net
	stat_busy "Starting VMware services"
	vmware-networks --start 2>&1 >>/var/log/vmware.log
	if [ $? -gt 0 ]; then
		stat_fail
		exit 1
	else
		stat_done
	fi
}

stop() {
	stat_busy "Stopping VMware USB Arbitrator" | tee -a /var/log/vmware.log
	killall --wait vmware-usbarbitrator  2>&1 >>/var/log/vmware.log
	if [ $? -gt 0 ]; then
		stat_fail
		exit 1
	else
		stat_done
	fi
	stat_busy "Stopping VMware services"
	vmware-networks --stop  2>&1 >>/var/log/vmware.log
	modprobe -r $MODULES
	if [ $? -gt 0 ]; then
		stat_fail
		exit 1
	else
		stat_done
	fi
}
remove() {
	stat_busy "Starting Remove VMware Kernel Modules" | tee -a /var/log/vmware.log
	for mod in $MODULES; do
		rm -f /lib/modules/$kernver/misc/$mod.ko 2>&1 >/dev/null
	done
	rm -f /etc/udev/rules.d/60-vmware.rules 2>&1 >/dev/null
	stat_done
}

case "$1" in
  start) start
    ;;
  stop) stop
    ;;
  restart) stop
    sleep 3
    start
    ;;
  remove) remove
    ;;
  *)
    echo "usage: $0 {start|stop|restart|remove}"
    ;;
esac

exit 0